package com.jianbing.shasha.system;

import com.jianbing.shasha.domain.ShashaLevelSetting;
import com.jianbing.shasha.domain.ShashaUserInfo;
import com.jianbing.shasha.mapper.ShashaLevelSettingMapper;
import com.jianbing.shasha.mapper.ShashaQuestionBankMapper;
import com.jianbing.shasha.mapper.ShashaUserRecordMapper;
import com.jianbing.shasha.service.IShashaUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 定时任务
 * shasha项目更新用户游戏进度
 * @param
 * @return 
 * @author zheng
 * @create 2024/6/10
 **/

@Component("shashaUpdateUserProcess")
public class ShashaUpdateUserProcess {

    @Autowired
    private IShashaUserInfoService shashaUserInfoService;
    @Autowired
    private ShashaLevelSettingMapper shashaLevelSettingMapper;
    @Autowired
    private ShashaUserRecordMapper shashaUserRecordMapper;
    @Autowired
    private ShashaQuestionBankMapper shashaQuestionBankMapper;

    public void updateAll(){
        ShashaUserInfo shashaUserInfo = new ShashaUserInfo();
        List<ShashaUserInfo> list = shashaUserInfoService.selectShashaUserInfoList(shashaUserInfo);
        for(ShashaUserInfo userInfo:list){
            Long times = shashaUserRecordMapper.selectDoQuesCountByUserIdAndLevel(userInfo.getUserId(), userInfo.getLevelProcessId());
            Integer flagNumber = userInfo.getLevelProcessId()>=25?18:30;

            if(userInfo.getRemainingLevels()==-1 || times>=flagNumber ||
                    times>=shashaQuestionBankMapper.getQuesWithCount(userInfo.getLevelProcessId()).size()){
                Long level = userInfo.getLevelProcessId()+1;
                ShashaUserInfo tmp = new ShashaUserInfo();
                tmp.setUserId(userInfo.getUserId());
                tmp.setLevelProcessId(level);
                if(shashaLevelSettingMapper.selectShashaLevelSettingById(level)!=null){
                    ShashaLevelSetting shashaLevelSetting =  shashaLevelSettingMapper.selectShashaLevelSettingById(level);
                    tmp.setRemainingLevels(shashaLevelSetting.getEndNum()-shashaLevelSetting.getBeginNum() + 1 );
                    shashaUserInfoService.updateShashaUserInfo(tmp);
                }
//            } else if (userInfo.getRemainingLevels() == 0) {
//                ShashaUserInfo tmp = new ShashaUserInfo();
//                tmp.setUserId(userInfo.getUserId());
//                tmp.setRemainingLevels(1L);
//
//                shashaUserInfoService.updateShashaUserInfo(tmp);
//            }
        }
        }
    }

}
